package com.ruoyi.project.jxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.jxy.domain.Entity;
import com.ruoyi.project.jxy.service.IEntityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jxy.domain.SlotEntityRef;
import com.ruoyi.project.jxy.service.ISlotEntityRefService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 【槽位与实体对应关系】Controller
 *
 * @author ruoyi
 * @date 2019-12-06
 */
@RestController
@RequestMapping("/jxy/ref")
public class SlotEntityRefController extends BaseController {
    @Autowired
    private ISlotEntityRefService slotEntityRefService;
    @Autowired
    private IEntityService entityService;

    /**
     * 查询【槽位】列表
     */
    @PreAuthorize("@ss.hasPermi('jxy:ref:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlotEntityRef slotEntityRef) {
        startPage();
        Map map = new HashMap();
        if (StringUtils.isNotBlank(slotEntityRef.getType())) {
            map.put("type", slotEntityRef.getType());
        }
        if (slotEntityRef.getIntentionId() != null) {
            map.put("intentionId", slotEntityRef.getIntentionId());
        }
        List<Map> list = slotEntityRefService.getSlotEntityList(map);
        return getDataTable(list);
    }

    /**
     * 查询意图引用的实体/辅助词
     */
    @PreAuthorize("@ss.hasPermi('jxy:ref:list')")
    @GetMapping("/listEntityRef")
    public TableDataInfo listEntityRef(SlotEntityRef slotEntityRef) {
        startPage();
        Map map = new HashMap();
        if (StringUtils.isNotBlank(slotEntityRef.getType())) {
            map.put("type", slotEntityRef.getType());
        }
        if (slotEntityRef.getIntentionId() != null) {
            map.put("intentionId", slotEntityRef.getIntentionId());
        }
        //非空数据
        map.put("refEntity", "true");
        List<Map> list = slotEntityRefService.getSlotEntityList(map);
        return getDataTable(list);
    }

    /**
     * 获取【槽位与实体对应关系】详细信息
     */
    @PreAuthorize("@ss.hasPermi('jxy:ref:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(slotEntityRefService.getById(id));
    }

    /**
     * 新增【槽位与实体对应关系】
     */
    @PreAuthorize("@ss.hasPermi('jxy:ref:add')")
    @Log(title = "【槽位与实体对应关系】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlotEntityRef slotEntityRef) {
        return toAjax(slotEntityRefService.save(slotEntityRef) ? 1 : 0);
    }

    /**
     * 修改【槽位与实体对应关系】
     */
    @PreAuthorize("@ss.hasPermi('jxy:ref:edit')")
    @Log(title = "【槽位与实体对应关系】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlotEntityRef slotEntityRef) {
        if (slotEntityRef.getEntityId() == null) {
            return AjaxResult.error("实体/辅助词id为空");
        }
       /* Entity entity = entityService.getById(slotEntityRef.getEntityId());
        if (entity == null) {
            return AjaxResult.error("该实体/辅助词不存在");
        } else {
            slotEntityRef.setType(entity.getType());
        }*/
        /*
        if (StringUtils.isBlank(slotEntityRef.getType())) {
            return AjaxResult.error("类型为空");
        }*/
        if (slotEntityRef.getId() == null) {
            return AjaxResult.error("槽位与实体关系id为空");
        }
        return toAjax(slotEntityRefService.updateSlotEntityInfo(slotEntityRef));
    }
}
